// ------------------------
// projects/life/TestLife.h
// Copyright (C) 2009
// Glenn P. Downing
// ------------------------

#ifndef TestLife_h
#define TestLife_h

// --------
// includes
// --------

#include "cppunit/TestFixture.h"             // TestFixture
#include "cppunit/extensions/HelperMacros.h" // CPPUNIT_TEST, CPPUNIT_TEST_SUITE, CPPUNIT_TEST_SUITE_END
#include <iostream>

// --------
// TestLife
// --------

struct TestLife : CppUnit::TestFixture {
    // -----
    // tests
    // -----

    void test_Output(){
	Life<ConwayCell> l(3, 3);
	std::ostringstream o;
	o<<l;
	CPPUNIT_ASSERT(o.str().compare("\nGeneration = 0, Population = 0.\n...\n...\n...\n")==0);}

    void test_readConway1(){
	Life<ConwayCell> l(3, 3);
	std::istringstream in;
	in.str("...\n...\n...\n");
	l.readConwayorFredkin(in);
	std::ostringstream o;
	o<<l;
	CPPUNIT_ASSERT(o.str().compare("\nGeneration = 0, Population = 0.\n...\n...\n...\n")==0);}

    void test_readConway2(){
	Life<ConwayCell> l(3, 3);
	std::istringstream in;
	in.str("***\n.*.\n.*.\n");
	l.readConwayorFredkin(in);
	std::ostringstream o;
	o<<l;
	CPPUNIT_ASSERT(o.str().compare("\nGeneration = 0, Population = 5.\n***\n.*.\n.*.\n")==0);}

    void test_conwayTurn(){
	Life<ConwayCell> l(3, 3);
	std::istringstream in;
	in.str("***\n.*.\n.*.\n");
	l.readConwayorFredkin(in);
	l.turn();
	l.turn();
	std::ostringstream o;
	o<<l;
	CPPUNIT_ASSERT(o.str().compare("\nGeneration = 2, Population = 2.\n.*.\n.*.\n...\n")==0);}

    void test_Fredkin(){
	Life<FredkinCell> l(3, 3);
	std::istringstream in;
	in.str("-0-\n-0-\n-0-\n");
	l.readConwayorFredkin(in);
	l.turn();
	std::ostringstream o;
	o<<l;
	CPPUNIT_ASSERT(o.str().compare("\nGeneration = 1, Population = 8.\n010\n0-0\n010\n")==0);}

    void test_cellGrid(){
	Life<Cell> l(3,3);
	std::istringstream in;
	in.str(".1-\n*.-\n0**\n");
	l.readCellGrid(in);
	l.turn();
	l.turn();
	l.turn();
	ostringstream o;
	o<<l;	
	CPPUNIT_ASSERT(o.str().compare("\nGeneration = 3, Population = 3.\n.*-\n..*\n0..\n")==0);}

    // -----
    // suite
    // -----

    CPPUNIT_TEST_SUITE(TestLife);
    CPPUNIT_TEST(test_Output);
    CPPUNIT_TEST(test_readConway1);
    CPPUNIT_TEST(test_readConway2);
    CPPUNIT_TEST(test_conwayTurn);
    CPPUNIT_TEST(test_Fredkin);
    CPPUNIT_TEST(test_cellGrid);
    CPPUNIT_TEST_SUITE_END();};

#endif // TestLife_h
